package com.vivo.ic.dm;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import com.vivo.ic.BaseLib;
import com.vivo.ic.VLog;
import com.vivo.ic.spmanager.VivoPreference;
import com.vivo.ic.spmanager.VivoPreferenceManager;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

public class DMUtil {
    private static final String TAG = Constants.PRE_TAG + "DMUtil";
    private static VivoPreference sPreference = VivoPreferenceManager.getInstance().getPreference();
    private static ArrayList<String> mDownloadingIds = new ArrayList<String>();
    
    public static NetworkInfo getActiveNetworkInfo() {
        ConnectivityManager connectivity = (ConnectivityManager) BaseLib.getContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            VLog.w(TAG, "couldn't get connectivity manager");
            return null;
        }
        
        final NetworkInfo activeInfo = connectivity.getActiveNetworkInfo();
        if (activeInfo == null) {
            VLog.v(TAG, "network is not available");
        }
        return activeInfo;
    }
    
    public static int getNetValueNow() {
        VLog.d(TAG, "getNetValueNow()");
        NetworkInfo netInfo = getActiveNetworkInfo();
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
            if (netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return Constants.CHANGE_STATE_WIFI;
            } else if (netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                return Constants.CHANGE_STATE_MOBILE;
            }
        }
        return Constants.CHANGE_STATE_NONE;
    }
    
    public static String normalizeMimeType(String type) {
        if (type == null) {
            return null;
        }
        
        type = type.trim().toLowerCase(Locale.ROOT);
        
        final int semicolonIndex = type.indexOf(';');
        if (semicolonIndex != -1) {
            type = type.substring(0, semicolonIndex);
        }
        return type;
    }
    
    public static void deleteFileIfExists(String path) {
        if (!TextUtils.isEmpty(path)) {
            VLog.d(TAG, "deleteFileIfExists() deleting " + path, new Throwable());
            final File file = new File(path);
            if (file.exists() && !file.delete()) {
                VLog.w(TAG, "file: '" + path + "' couldn't be deleted", new Throwable());
            }
        }
    }
    
    public static void closeQuietly(Closeable in) {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                VLog.e(TAG, "closeQuietly error " + e);
            }
        }
    }
    
    public static void closeQuietly(Cursor in) {
        if (in != null && !in.isClosed()) {
            in.close();
        }
    }
    
    /**
     * 设置“不再提示”
     * 
     * @param neverRemind
     */
    public static void setNeverRemind(boolean neverRemind) {
        sPreference.putBoolean(Constants.SP_NET_WARN_KEY, neverRemind);
    }
    
    /**
     * “不再提示”
     * 
     * @return
     */
    public static boolean isNeverRemind() {
        return sPreference.getBoolean(Constants.SP_NET_WARN_KEY, false);
    }


    public static void addDownloadingId(long id) {
        String sid = String.valueOf(id);
        if (!mDownloadingIds.contains(sid)) {
            mDownloadingIds.add(sid);
        }
    }

    public static boolean isIdDownloading(long id) {
        String sid = String.valueOf(id);
        return mDownloadingIds.contains(sid);
    }

    public static void removeDownloadingId(long id) {
        mDownloadingIds.remove(String.valueOf(id));
    }
    
}
